package DouBan

class Test {
    fun maxSubArray(nums: IntArray) : Int{
        val len = nums.size
        var dp = intArrayOf(len)

        dp[0] = nums[0]

        for (i in 1 until len){
            when{
                dp[i - 1] > 0  -> dp[i] = dp[i - 1] + nums[i]
                else -> dp[i] = nums[i]
            }
        }

        var res = dp[0]
        for (i in 1 until len){
            res = Math.max(res, dp[i])
        }
        return res
    }
}

// 使用kotlin实现最大子序和
fun main(){
    for(i in 1 until 10){
        println(i)
    }
    var arr = intArrayOf(1,2,3,-4,8,2,-6)
    var maxSubArray = Test().maxSubArray(arr);
    println(maxSubArray)
}